﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinesys.Entities;
using Chinesys.DataModel;
using Chinesys.Domain.DataMapper;
using Chinesys.Domain.InputValidations;
using Chinesys.Domain.BizRules;

namespace Chinesys.Domain
{
    public static class RoleDomain
    {
        public static List<RoleEntity> GetAll()
        {
            List<RoleEntity> list = new List<RoleEntity>();

            using (ChinesysEntities db = new ChinesysEntities())
            {
                List<Role> listRole = db.Roles.ToList();
                listRole.ForEach(x => list.Add(RoleDataMapper.RoleFromRowToEntity(x)));
            }

            return list;
        }

        public static RoleEntity GetById(int id)
        {
            RoleEntity role;
            using (ChinesysEntities db = new ChinesysEntities())
            {
                Role result = db.Roles.SingleOrDefault(x => x.Id == id);
                role = RoleDataMapper.RoleFromRowToEntity(result);
            }

            return role;
        }

        public static RoleEntity GetByRoleName(string roleName)
        {
            RoleEntity role;
            using (ChinesysEntities db = new ChinesysEntities())
            {
                Role result = db.Roles.SingleOrDefault(x => x.RoleName == roleName);
                role = RoleDataMapper.RoleFromRowToEntity(result);
            }

            return role;
        }

        private static RoleEntity Insert(RoleEntity roleEntity)
        {
            using (ChinesysEntities db = new ChinesysEntities())
            {
                Role row = RoleDataMapper.RoleFromEntityToRow(roleEntity);
                db.AddToRoles(row);
                roleEntity.Id = row.Id;
            }

            return roleEntity;
        }

        private static RoleEntity Update(RoleEntity entity)
        {
            using (ChinesysEntities db = new ChinesysEntities())
            {
                Role row = db.Roles.SingleOrDefault(x => x.Id == entity.Id);

                if(row == null)
                    throw new BizRulesException("Error, El Role No se encuentra registrado, no se puede generar la modificación.");

                row.Active = entity.Active;
                row.RoleDescription = entity.Description;
                row.RoleName = entity.RoleName;

                db.SaveChanges();
            }

            return entity;
        }

        public static RoleEntity Save(RoleEntity entity)
        {
            RoleInputValidation validator = new RoleInputValidation(entity);

            if(!validator.IsValid)
                throw new InputValidationException("Hubo errores de validación", validator);

            RoleBizRules.OnSave(entity);

            if (entity.CurrentState == RowState.New)
                entity = Insert(entity);

            if (entity.CurrentState == RowState.Modified)
                entity = Update(entity);

            return entity;
        }
    }
}
    